package com.and.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.and.adapter.FriendAdapter;
import com.and.dao.PageDao;
import com.and.dao.impl.PageDaoImpl;
import com.and.main.R;
import com.and.vo.Friend;

/**
 * @Title: FriendList.java
 * @Package com.and.ui
 * @Description: TODO(添加描述)
 * @author hanchunping
 * @date 2010-7-5 下午09:45:25
 * @version V1.0
 */
public class FriendList extends ListActivity {
	Button firstBtn;
	Button beforeBtn;
	Button nextBtn;
	Button afterBtn;
	Button gotoTip;
	EditText gotoPage;
	ListView listview;
	List<Friend> flist;
	List<Friend> clist;
	FriendAdapter fa;
	PageDao pd;
	Menu mMenu;
	SharedPreferences.Editor editor;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initFriendList();
	    pd = new PageDaoImpl(this.flist,10);
	    this.clist = (List<Friend>)pd.getCurrentList();
		setContentView(R.layout.main);
		fa = new FriendAdapter(this, this.clist);
		init();
		setListiner();
		setListAdapter(fa);
	}

	/**
	 * 
	 * @Title: initFriendList
	 * @Description: 初始化List数据
	 */
	private void initFriendList() {
		flist = new ArrayList<Friend>();
		for (int i = 0; i < 25; i++) {
			Friend f = new Friend();
			f.setId(i);
			f.setName("name_" + i);
			f.setEmail("email_" + i);
			f.setImageUrl("imageUrl_" + i);
			this.flist.add(f);

		}
	}
	/**
	 * 
	 * @Title: init
	 * @Description: 初始化
	 */
	private void init(){
		firstBtn =(Button)findViewById(R.id.firstBtn) ;
		beforeBtn =(Button)findViewById(R.id.beforeBtn) ;
		nextBtn =(Button)findViewById(R.id.nextBtn) ;
		afterBtn=(Button)findViewById(R.id.afterBtn) ;
		gotoPage =(EditText)findViewById(R.id.gotoPage) ;	
	}
	/**
	 * 
	 * @Title: setListiner
	 * @Description: 为按钮设置监听
	 */
	private void setListiner() {
		firstBtn.setOnClickListener(firstListener);
		beforeBtn.setOnClickListener(beforeListener);
		nextBtn.setOnClickListener(nextListener);
		afterBtn.setOnClickListener(afterListener);	
	}
	/**
	 * 监听下页按钮
	 */
	private OnClickListener nextListener = new OnClickListener(){
		@Override
		public void onClick(View view) {
			pd.nextPage();
			clist = (List<Friend>)pd.getCurrentList();
			fa.initList(clist);
			fa.notifyDataSetChanged();
		}
		
	};
	/**
	 * 监听上一页按钮
	 */
	private OnClickListener beforeListener = new OnClickListener(){
		@Override
		public void onClick(View view) {
			pd.prePage();
			clist = (List<Friend>)pd.getCurrentList();
			fa.initList(clist);
			fa.notifyDataSetChanged();
		}
		
	};
	/**
	 * 监听首页按钮
	 */
	private OnClickListener firstListener = new OnClickListener(){
		@Override
		public void onClick(View view) {
			pd.headPage();
			clist = (List<Friend>)pd.getCurrentList();
			fa.initList(clist);
			fa.notifyDataSetChanged();
		}
		
	};
	/**
	 * 监听尾页按钮
	 */
	private OnClickListener afterListener = new OnClickListener(){
		@Override
		public void onClick(View view) {
			pd.lastPage();
			clist = (List<Friend>)pd.getCurrentList();
			fa.initList(clist);
			fa.notifyDataSetChanged();
		}
		
	};
	/**
	 * 获取监听事件
	 */
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		
	}
	@Override
	 public boolean onCreateOptionsMenu(Menu menu){
		mMenu = menu;
		
		MenuInflater mInflater = getMenuInflater();
		mInflater.inflate(R.menu.menu, menu);
		return true;
		 
	 }
	@Override
	 public boolean onOptionsItemSelected(MenuItem item){
		switch(item.getItemId()){
		case R.id.logoutMenuItem:
			Intent i = new Intent(getApplicationContext(),LoginUI.class);
			startActivity(i);
			finish();
			return true;
			default:
				if(!item.hasSubMenu()){
					Toast.makeText(this, item.getTitle(), Toast.LENGTH_LONG).show();
					return true;
				}
			break;
		}
		return false;
		 
	 }
}
